
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>门店产品资料</title>
	<link rel="stylesheet" type="text/css" href="../../rs/plugins/easyui/themes/metro/easyui.css">
	<link rel="stylesheet" type="text/css" href="../../rs/plugins/easyui/themes/icon.css">
	<link href="../../rs/css/bootstrap.min.css" rel="stylesheet">

	<link href="../../rs/plugins/datapicker/datepicker3.css" rel="stylesheet">
	<script type="text/javascript" src="../../rs/plugins/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="../../rs/plugins/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../rs/plugins/easyui/locale/easyui-lang-zh_CN.js"></script>
	<script src="../../rs/js/jquery.cookie.js"></script>
	<script src="../../rs/js/common_qj.js"></script>
	<style>
		html,body{ margin:0px; height:100%;}
	</style>
	<?php
 	 require_once('login.php');
	 ?>
</head>
<body >
<table id="table" class="easyui-datagrid"  style="width:100%;height:100%;padding: 10px;"
	   data-options="
			rownumbers:true,
			checkOnSelect:true,
			method:'get' ,
			toolbar:'#easyui_toolbar',
			fit:true,
			fitColumns:true,
	    iconCls: 'icon-edit'
	        ">
	<thead>
	<tr>
		<th data-options="field:'ck',checkbox:true"></th>
		<th data-options="field:'prid',width:20,align:'center'">产品编号</th>
		<th data-options="field:'shopname',width:20,align:'left'">门店名称</th>
		<th data-options="field:'prname1',width:20,align:'left'">产品名称</th>
		<th data-options="field:'dj',width:15,align:'center',formatter:formatId">单价</th>
		<th data-options="field:'bz',width:25,align:'center'">备注</th>
	</tr>
	</thead>
</table>

<div id="easyui_toolbar" region="north" border="false"
	 style="height: 45px;padding: 5px 5px; ">

	<div class="row">
		<div class="col-sm-4"  >
			<div class=" draggable">
				<div class="input-group ">
				<span class="input-group-btn">
					<button id="shanchu" style="margin-right: 10px;height:34px;font-size: 14px;border-radius: 0px" type="button" class="btn btn-sm " onclick="data_remove()"> <i class="glyphicon glyphicon-trash"></i> </button>
					<button style="margin-right: -2px;height:34px;background-color: #FFF;border: 1px solid #e5e6e7;font-size: 14px" type="button" class="btn btn-sm ">门店</button>
				</span>
					<select class="form-control" id="mdbh">
						<option value="">所有门店</option>
					</select>
				</div>
			</div>
		</div>
		<div class="col-sm-4"></div>
		<div class="col-sm-4">
			<div class=" draggable">
				<div class="input-group ">
					<input type="text" class="form-control" id="searchtext" placeholder="输入搜索关键字"  >
					<span class="input-group-btn">
						<button type="button" onclick="do_search()" style="border-radius: 0px; height: 34px;"   class="btn">
							<i class="glyphicon glyphicon-zoom-in"></i> 查询
						</button>
						<button id="xinzeng" type="button"  style=" margin-left:5px; border-radius: 0px; height: 34px;" class="btn  " onclick="open_modal()">
							<i class="glyphicon glyphicon-plus"></i> 新增
						</button>
				</span>
				</div>
			</div>
		</div>

	</div>
</div>

<div style="display: none" id="open_edit">
	<form id="edit_from">
		<div class="input-group" style="display:none">
			<div class="input-group-btn">
				<button style="margin-right: -2px; height:34px" type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown"> 企业编号</button>
			</div>
			<input type="text" class="form-control "   name="rid"    >
		</div>
		<div class="input-group" style="padding: 20px 20px 10px 20px;">
			<div class="input-group-btn">
				<button style="margin-right: -2px; height:34px" type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">单价</button>
			</div>
			<input type="text" class="form-control "  name="dj"  id="xg_dj"    required="true"  >
		</div>
		<div class="input-group" style="padding: 0px 20px 10px 20px;">
			<div class="input-group-btn">
				<button style="margin-right: -2px; height:34px" type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">备注</button>
			</div>
			<input type="text" class="form-control" name="bz" id="xg_bz"  >
		</div>


		<div class="lay_foot">
			<button type="reset" id="reset" style="display:none;">reset</button>
			<button type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown"
					style="background-color: #e2e2e2;" onclick="layer.closeAll()">返回
			</button>
			<button type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown"
					style="background-color: #f54141;color: white;float: right" id="save_dj">保存
			</button>
		</div>
	</form>
</div>
<div id="easyui_toolbar_xz" region="north" border="false"
	 style="height: 45px;padding: 5px 5px; ">
	<div id="tb" class="input-group" style="float: right; margin-right: 10px;width:280px">
		<input type="text" class="form-control" id="searchtext_table_xz" placeholder="输入搜索关键字" value="">
		<div class="input-group-btn">
			<button type="button" class="btn  dropdown-toggle" onclick="f_sheard_xz()" style="height:34px;border-radius: 0px;">
				搜索
			</button>
		</div>
	</div>

</div>
<div style="display: none" id="open_table_xz">
	<div id="edit_table_xz">
		<table id="table_xz" class="easyui-datagrid"  style="width:100%;height:520px;padding: 10px;"
			   data-options="
			rownumbers:true,
			singleSelect:'false',
			toolbar:'#easyui_toolbar_xz',
			method:'get' ,
			height: 250,
			fit:false,
			fitColumns:true
	        ">
			<thead>
			<tr>
				<th data-options="field:'prid',width:20,align:'center'">商品编号</th>
				<th data-options="field:'prname1',width:50,align:'center'">商品名称</th>
				<th data-options="field:'prpriceout',width:20,align:'left'">成本价</th>
				<th data-options="field:'xz',width:15,align:'center',formatter:format_xz"></th>
			</tr>
			</thead>
		</table>

	</div>
</div>








<div style="display: none" id="open_xz">
	<form id="edit_xz">
		<div class="input-group" style="display:none">
			<div class="input-group-btn">
				<button type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown"> 企业编号</button>
			</div>
			<input type="text" class="form-control "   name="rid"  >
		</div>
		<div class="input-group" style="padding: 20px 20px 15px 20px;">
			<div class="input-group-btn">
				<button style="margin-right: -2px; height:34px" type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">产品</button>
			</div>
					<input   type="text" class="form-control" name="prname1" id="searchtext_xz" placeholder="输入搜索关键字"  >
					<span class="input-group-btn">
						<button  type="button" onclick="f_sheard_xz_btn()" style="border-radius: 0px; height: 34px;"   class="btn btn-white">
							<i class="glyphicon glyphicon-zoom-in"></i>
						</button>
				</span>


		</div>

		<div class="input-group" style="padding: 0px 20px 10px 20px;">
			<div class="input-group-btn">
				<button style="margin-right: -2px; height:34px" type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">门店</button>
			</div>
			<select class="form-control"   name="mdbh1" id="mdbh1"></select>
		</div>

		<!--<div class="input-group" style="padding: 0px 20px 10px 20px;">
				<span class="input-group-btn">
					<button style="margin-right: -2px; height:34px" type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">产品</button>
				</span>
			<input type="text" class="form-control" id="prname1" name="prname1" required="false">
			<div class="input-group-btn">
				<ul class="dropdown-menu dropdown-menu-right" role="menu" style="min-width: 300px !important;">
				</ul>
			</div>
		</div>-->
		<div class="input-group" style="padding: 0px 20px 10px 20px;">
			<div class="input-group-btn">
				<button style="margin-right: -2px; height:34px" type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">单价</button>
			</div>
			<input type="text" class="form-control "   name="dj"  id="dj"   >
		</div>

		<div class="input-group" style="padding: 0px 20px 10px 20px;">
			<div class="input-group-btn">
				<button style="margin-right: -2px; height:34px" type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">备注</button>
			</div>
			<input type="text" class="form-control "   name="bz" id="bz_xz">
		</div>


		<div class="lay_foot">
			<button type="reset" id="reset_xz" style="display:none ">reset</button>
			<button type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown"
					style="background-color: #e2e2e2;" onclick="layer.closeAll()">返回
			</button>
			<button type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown"
					style="background-color: #f54141;color: white;float: right" id="save_do">保存
			</button>
		</div>
	</form>
</div>

<!--页面加载缓冲图标-->
<div id='loadingDiv' style="position:absolute;z-index:1000;top:0px; left:0px;
	width:100%;height:100%;background:white;text-align:center;">
	<h1 style="top: 30%; position: relative;">
		<img src="../../rs/img/loading-2.gif" /><br>
	</h1>
</div>
<script src="../../rs/plugins/datapicker/bootstrap-datepicker.js"></script>
<script>

	/*页面加载缓冲*/
	function closeLoading() {
		$("#loadingDiv").fadeOut("normal", function () {
			$(this).remove();
		});
	}
	var no;
	$.parser.onComplete = function () {
		if (no) clearTimeout(no);
		no = setTimeout(closeLoading, 1000);
	}

	var sign = URLencode($.cookie('sign'));
	//    日期插件js开始
	$("#data_5 .input-daterange").datepicker({keyboardNavigation: !1, forceParse: !1, autoclose: !0});
	var config = {
		".chosen-select": {},
		".chosen-select-deselect": {allow_single_deselect: !0},
		".chosen-select-no-single": {disable_search_threshold: 10},
		".chosen-select-no-results": {no_results_text: "Oops, nothing found!"},
		".chosen-select-width": {width: "95%"}
	};
	//    日期插件js结束

	$(function(){
		if(sign==="undefined"){
			window.location.href = "login.html";
		}
		sel_mdbh();
		f_sheard();
    sqx();//判断小权限
		var getdata;

	});




	//判断用户权限&加载数据
	function f_sheard(){
		$.ajax({
			type: "post",
			url:G_url+'prsh/see?sign='+sign,
			data:'',
			dataType: "json",
			success: function (result){
			  getdata=result;
			  $('#table').datagrid({
					data: result.data
				});

			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
			}
		});
	}



	// 产品搜索操作
	$('#searchtext').bind('keypress',function(event){
		if(event.keyCode == "13") {
			do_search();
		}
	});
	function do_search(){
		var gjc=$('#searchtext').val(); //搜索关键词
		if(gjc==""){
			$('#table').datagrid({
				data: getdata.data
			});
		}else{
			var newdata=[];
			for(var i=0;i<getdata.data.length;i++){
				str=transform(getdata.data[i]).join(',');
				row=str.indexOf(gjc);
				if(row>=0){
					newdata.push(getdata.data[i]);
				}
			}

			$('#table').datagrid({
				data: newdata
			});
		}
	}
	function transform(obj){
		var arr = [];
		for(var item in obj){
			arr.push(obj[item]);
		}
		return arr;
	}

  function sel_mdbh(){
	  $.ajax({
		  type: "post",
		  url:G_url+'Prsh/getshop',
		  data:'sign='+sign,
		  dataType: "json",
		  success: function (data){
			  //给门店编号做循环
			  var ff = data.rows;
			  var html = '';
			  $.each(ff, function (a, b) {//直接取出json的prolist做each循环.
				  html += "<option value='"+b.shopid+"'>" + b.shopname + "</option>";//拼接字符串.
			  });
			  $("#mdbh").append(html);
			  $("#mdbh1").append(html);
		  },
		  error: function (XMLHttpRequest, textStatus, errorThrown) {
		  }
	  });
  }

	var prid;
	var prname1;
	$("#save_do").on("click",function(){
		var shopname=$('#mdbh1').find("option:selected").text();
		var shid=$('#mdbh1').val();
		var dj=$('#dj').val();
		var bz_xz=$('#bz_xz').val();
		if (prid==undefined)
			{
				layer.msg("请搜索您要添加的商品！");
			}else{
				$.ajax({
					type: "post",
					url:G_url+'prsh/save?sign='+sign,
					data:"shid="+shid+"&prid="+prid+"&dj="+dj+"&bz="+bz_xz,
					dataType: "json",
					success: function (result){
						if (result.ok){
							$('#table').datagrid('appendRow',{
								prid:prid,
								shopname:shopname,
								prname1:prname1,
								dj:dj,
								bz:bz_xz
							});
							layer.msg(result.msg);
						} else {
							layer.msg(result.msg);
						}
					},
					error: function (XMLHttpRequest, textStatus, errorThrown) {
					}
				});
		}

	});

	$("#save_dj").on("click",function(){
		var rid=$("input[name='rid']").val();
		var dj=$("#xg_dj").val();
		var bz=$("#xg_bz").val();
		$.ajax({
			type: "post",
			url:G_url+'prsh/save2?sign='+sign,
			data:"rid="+rid+"&dj="+dj+"&bz="+bz,
			dataType: "json",
			success: function (result){
				if (result.ok){
					$('#table').datagrid('updateRow',{
						index: rowindex,
						row: {
							dj: dj,
							bz: bz
						}
					});
					layer.msg(result.msg);
				} else {
					layer.msg(result.msg);
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
			}
		});
		layer.closeAll();

	});
    var rowindex;
	function open_edit(r){
    sqx_save_prsh("修改");
		var rowdata=$('#table').datagrid('getData');
		var row=rowdata.rows[r];
		rowindex=r;
		if (row){
			$("#open_edit").show();
			$('#edit_from').form('load',row);
			layer.open({
				title: "修改",
				type: 1,
        offset: 'rb',

        area: ['40%', '100%'], //宽高
				shadeClose: true,
				content: $('#edit_from')
			});
		}
	}
	function formatId(value,rowData,rowIndex){
		return "<a href='javascript:void(0)' onclick='open_edit("+rowIndex+");'>"+value+"</a>";
	}


	function open_modal(){
    sqx_save_prsh("新增");
		$("#open_xz").show();
		$('#reset_xz').click();
		table=layer.open({
			title: "新增",
			type: 1,
      offset: 'rb',
      area: ['40%', '100%'], //宽高
      shadeClose: true,
			content: $('#edit_xz')
		});
	}


	//弹出选择产品表格
	function open_table_modal(){
		$("#open_table_xz").show();
		/*$('#reset_xz').click();*/
		table_xz=layer.open({
			title: "添加商品",
			type: 1,
      offset: 'rb',


      area: ['50%', '100%'], //宽高
      shadeClose: true,
			content: $('#edit_table_xz')
		});
	}

	/*删除操作*/
	function data_remove(){
		var rows = $('#table').datagrid('getSelections');

		if(rows.length!=0){
			layer.msg('确定删除吗？', {
				time:0,
				btn: ['确定', '取消'] //按钮
				,yes: function do_remove() {
				var rids = $.map(rows, function (row) {
					return row.rid;
				});
				$.ajax({
					type: "post",
					url:G_url+'prsh/del?sign='+sign,
					data:"&rid=" + rids,
					dataType: "json",
					success: function (result) {
						if (result.ok) {
							layer.closeAll();
							f_sheard();
							layer.msg(result.msg);

						} else {
							layer.msg(result.msg);
						}
					},
					error: function (XMLHttpRequest, textStatus, errorThrown) {
						//alert(errorThrown);
					}
				});
			}
			}
			);
		}else{
			layer.msg("请先选择！");
		}
	}
	//删除操作

	$('#searchtext_xz').bind('keydown',function(event){
		if(event.keyCode ==13) {
			f_sheard_xz();
		}
	});

	//搜索结果的数据加载
	function f_sheard_xz(){
		var gjc_xz=$('#searchtext_xz').val(); //搜索关键词
		$.ajax({
			type: "post",
			url:G_url+'prsp/getslpr_search?sign='+sign+'&s='+gjc_xz,
			data:'',
			dataType: "json",
			success: function (result){
				if(result.rows.length==1){
					$("input[name='prname1']").val(result.rows[0].prname1);
					$('#dj').val(result.rows[0].prpriceout);
					prid=result.rows[0].prid;
					prname1=result.rows[0].prname1;
				}else if(result.rows.length==0){
					layer.msg("数据为空!");
				}else{
					open_table_modal();
					/*layer.close(table);*/
					$('#table_xz').datagrid({
						data: result.rows
					});
				}


			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
			}
		});
	}


	function f_sheard_xz_btn(){
		$.ajax({
			type: "post",
			url:G_url+'prsp/getslpr_search?sign='+sign,
			data:'',
			dataType: "json",
			success: function (result){
					open_table_modal();
					$('#table_xz').datagrid({
						data: result.rows
					});
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
			}
		});
	}


	//表格内的搜索   searchtext_table_xz



	$('#searchtext_table_xz').bind('keydown',function(event){
		if(event.keyCode ==13) {
			searchtext_table_xz();
		}
	});

	//搜索结果的数据加载
	function searchtext_table_xz(){
		var gjc_xz=$('#searchtext_table_xz').val(); //搜索关键词
		$.ajax({
			type: "post",
			url:G_url+'prsp/getslpr_search?sign='+sign+'&s='+gjc_xz,
			data:'',
			dataType: "json",
			success: function (result){
				if(result.rows.length==0){
					layer.msg("搜索结果为空!");
				}else{
					$('#table_xz').datagrid({
						data: result.rows
					});
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
			}
		});
	}




	function format_xz(value,rowData,rowIndex){
		return "<button style='height: 24px;line-height: 24px;padding: 0px 6px;border-radius: 0px;background-color: #23c6c8;color: #fff;' class='btn' onclick='add_sp("+JSON.stringify(rowData)+");'>添加</button>";
	}

	function add_sp(rowdata){
		$("input[name='prname1']").val(rowdata.prname1);
		$('#dj').val(rowdata.prpriceout);
		prid=rowdata.prid;
		prname1=rowdata.prname1;
		layer.close(table_xz);
		/*layer.open(table);*/
	}


	$('#mdbh').change(function(){
		var mdbh=$(this).children('option:selected').val();//这就是selected的值
		layer.msg('数据加载中...', {icon: 16, time: 200000, shade: [0.1, 'black']});
		$.ajax({
			type: "post",
			url:G_url+'prsh/see?sign='+sign,
			data:'mdbh='+mdbh,
			dataType: "json",
			success: function (result){
				if (result.ok){
					getdata=result;
					$('#table').datagrid({
						data: result.data
					});
					layer.closeAll();
				} else {
					layer.msg(result.msg);
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
			}
		});
	})

</script>

<script src="../../rs/plugins/layer/layer.min.js"></script>
<script src="../../rs/js/bootstrap.js"></script>
<script src="../../rs/plugins/md5.js"></script>
<script src="../../rs/plugins/tableExport.js"></script>



</body>
</html>
